<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Xl extends MY_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();
				
		//Load Video Script
		$this->data['js'] = array('jquery-1.8.3.min.js');
		$this->data['css'] = array();
		$this->data['js'][] = "xl/xl.js";
		$this->data['js'][] = "xl/xl_common.js";
		$this->data['js'][] = "xl/xl_keycode.js";
		$this->data['css'][] = "xl/xl.css";
		$this->data['css'][] = "xl/xl_lge.css";				
    }
	
	/*Home*/	 
	public function index()
	{
		$this->data['js'][] = "xl/xl_main.js"; 
		$this->data['js'][] = "xl/xl_home.js";
		$this->data['content'] = $this->router->class."/index";
		$this->load->view("templates/".$this->folder_templates."templates_xl", $this->data);
	}
	
	/*Play*/
	public function play($id)
	{
		$this->data['js'][] = "xl/xl_play.js";
		$this->data['js'][] = "xl/xl_player.js";
		
		$this->data['dataVideo'] = $this->db->select('t.video_id,t.views,t.is_relate_song,t.video_publish_date,t.create_date,j.video_name,j.video_file_name,j.video_file_ext,j.video_thumb_name,j.video_thumb_ext')
		->join('video_info as j','j.video_id=t.video_id','INNER')
		->get_where('video as t',array('t.video_id' => $id),1)
		->row(); 
		if($this->data['dataVideo']->is_relate_song == 1):
			$this->data['dataVideo']->artist = $this->db->select('j.artist_id,j.artist_name')
			->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
			->join('artist_info as j','j.artist_id=k.artist_id','INNER')
			->get_where('video_song as t',array('video_id' => $this->data['dataVideo']->video_id))
			->result();			
		else:
			$this->data['dataVideo']->artist = $this->db->select('j.artist_id,j.artist_name')
			->join('artist_info as j','j.artist_id=t.artist_id','INNER')
			->get_where('video_artist as t',array('video_id' => $this->data['dataVideo']->video_id))
			->result();			
		endif;	
		
		//GA Events
		$this->data['ga_event'] = 'CI-'.$id.'-'.seo_url($this->data['dataVideo']->video_name);	
					
		$this->data['content'] = $this->router->class."/play";
		$this->load->view("templates/".$this->folder_templates."templates_xl", $this->data);
	}
	
	public function ajax_main()
	{
		$this->data['dataVideo'] = $this->db->select('t.video_id,t.views,j.video_name,j.video_thumb_name,j.video_thumb_ext')
		->join('video_info as j','j.video_id=t.video_id','INNER')
		->order_by('video_id','DESC')
		->get_where('video as t',array('t.video_type_id' => 1,'j.video_embed' => ''),1)
		->row(); 

		$this->data['dataVideoList'] = $this->db->select('t.video_id,t.views,j.video_name,j.video_thumb_name,j.video_thumb_ext')
		->join('video_info as j','j.video_id=t.video_id','INNER')
		->order_by('t.views','DESC')
		->get_where('video as t',"t.video_type_id = 1 AND j.video_embed = '' AND DATE(t.create_date) < CURDATE() AND DATE(t.create_date) >= DATE_SUB(CURDATE(),INTERVAL 15 DAY)",20)
		->result();	
		
		echo $this->load->view($this->router->class."/".$this->router->method,$this->data,TRUE);
		exit;		
	}

	public function ajax_sub_main($video_type_id,$page = NULL)
	{
		if($page == NULL) $page = 1;
		$offset = ($page == 1 ? 0:($page*10));
		$this->data['dataVideoList'] = 	$this->db->select('t.video_id,t.views,j.video_name,j.video_thumb_name,j.video_thumb_ext')
		->join('video_info as j','j.video_id=t.video_id','INNER')
		->order_by('video_id','DESC')
		->get_where('video as t',array('t.video_type_id' => $video_type_id,'j.video_embed' => ''),10,$offset)
		->result();	
			
		echo $this->load->view($this->router->class."/".$this->router->method,$this->data,TRUE);
		exit;		
	}	
}


/* End of file video.php */
/* Location: ./application/controllers/video.php */